package com.jams.goodox.widget;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.jams.goodox.R;
import com.jams.goodox.activity.wallet.PayConstants;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;
import com.jams.library.widget.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jams_zhen on 2017/5/11.
 */

public class PayView extends FrameLayout implements PayConstants,View.OnClickListener{
    private Context mContext;
    private ListCommonAdapter listCommonAdapter;
    private List<Integer> payTypeList;

    private int selectPosition;
    private int selectType;

    private OnPayChoose onPayChoose;

    public PayView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public PayView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PayView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.mContext=context;
        LayoutInflater.from(context).inflate(R.layout.pay_view,this,true);
        payTypeList=new ArrayList<>();
        MyListView listViewType=(MyListView)findViewById(R.id.lv_pay_type);
        listCommonAdapter=new ListCommonAdapter<Integer>(context,payTypeList,R.layout.pay_type) {
            @Override
            public void convert(ListCommonViewHolder helper, Integer item, int position) {
                ImageView imageView = helper.getView(R.id.iv_choose);
                if (selectPosition == position) {
                    imageView.setBackgroundResource(R.mipmap.check_theme);
                } else {
                    imageView.setBackgroundResource(R.mipmap.uncheck_icon);
                }
                switch (item) {
                    case ZFB://支付宝
                        helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_zfb);
                        helper.setText(R.id.tv_pay_name, "支付宝支付");
                        helper.setText(R.id.tv_pay_tip, "推荐有支付宝账户的用户使用");
                        break;
                    case YL://银联
                        helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_yl);
                        helper.setText(R.id.tv_pay_name, "银联支付");
                        helper.setText(R.id.tv_pay_tip, "各大银行无卡支付、快捷支付");
                        break;
                    case WX://微信
                        helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_wx);
                        helper.setText(R.id.tv_pay_name, "微信支付");
                        helper.setText(R.id.tv_pay_tip, "微信安全支付");
                        break;
                    case BALANCE://余额支付
                        helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_wx);
                        helper.setText(R.id.tv_pay_name, "微信支付");
                        helper.setText(R.id.tv_pay_tip, "微信安全支付");
                        break;
                }
            }
        };
        listViewType.setAdapter(listCommonAdapter);
        listViewType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                selectPosition=position;
                selectType=payTypeList.get(position);
                listCommonAdapter.notifyDataSetChanged();
            }
        });
        findViewById(R.id.tx_submit).setOnClickListener(this);
    }

    public void setOnPayChoose(OnPayChoose onPayChoose){
        this.onPayChoose=onPayChoose;
    }

    @Override
    public void onClick(View view) {
        if(onPayChoose!=null){
            onPayChoose.goPay(selectType);
        }
    }

    static public interface OnPayChoose {
        void goPay(int payType);
    }
}
